package com.lkm.app_ui.m.Imple.fun;

import com.google.gson.JsonObject;
import com.lkm.app.event.OnNetworkStateEvent;
import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.help.AppHelp;
import com.lkm.app_ui.help.GsonHelp;
import com.lkm.app_ui.help.PhoneInfoUtils;
import com.lkm.app_ui.help.TaskHelp;
import com.lkm.app_ui.help.WifiUtils;
import com.lkm.app_ui.netapi.AppApi;
import com.lkm.comlib.fun_m.NetworkStateModule;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.entity.ResponEntity;
import com.lkm.market2_0.m.steelyardserver.event.OnSteelyardValueEvent;
import com.lkm.market2_0.m.steelyarddata.SteelyardDataModule;
import com.lkm.market2_0.m.steelyarddata.SteelyardValue;
import com.lkm.market2_0.m.steelyardstatus.StatusInfo;
import com.lkm.market2_0.m.steelyardstatus.SteelyardStatusManagerModule;
import com.lkm.market2_0.task.DefaultTaskExecutor;

import java.util.Map;



public class RegBalanceTask extends TaskAdapter<SteelyardValue, String, Boolean> {


    

    public static void waitValue() {

        try {
            SteelyardDataModule mSteelyardDataModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);
            SteelyardValue steelyardValue = mSteelyardDataModule.getSteelyardValue(RunTimeData.steelyardInfo.getSteelyardMac());
            if(steelyardValue!=null){
                start(steelyardValue);
                return;
            }
        }catch (Exception e){

        }
        LinkBus.registerReceiver(new ObserverAutoNext<OnSteelyardValueEvent>() {
            @Override
            public void onReceiver(OnSteelyardValueEvent data) {
                LinkBus.unregisterReceiver(this);
                if (TaskHelp.getAllTask(RegBalanceTask.class.getName()) != null) {
                    
                } else {
                    start(data.getSteelyardValue());
                }
            }
            @Override
            public Class getBusEventClass() {
                return OnSteelyardValueEvent.class;
            }
        });

    }

    public static RegBalanceTask start(SteelyardValue steelyardValue) {
        RegBalanceTask regBalanceTask = new RegBalanceTask();
        regBalanceTask.action();
        regBalanceTask.setParam(steelyardValue);
        regBalanceTask.execTask();
        return regBalanceTask;
    }

    public RegBalanceTask() {
        super(new DefaultTaskExecutor(true), RegBalanceTask.class.getName(), AppConst.getTaskManager2());
    }

    @Override
    public void doExecuting(SteelyardValue param) {
        super.doExecuting(param);

        mTaskExecutor.executor(new Runnable() {
            @Override
            public void run() {
                ayncdoExecuting();
            }
        });

    }



    private void ayncdoExecuting() {


        SteelyardValue steelyardValue = getParam();
        Map<String, Object> stringStringMap = GsonHelp.fromJson(GsonHelp.toJson(steelyardValue),new com.google.gson.reflect.TypeToken<Map<String, String>>() {}.getType());


        StatusInfo statusInfo = null;
        SteelyardStatusManagerModule statusManagerModule = (SteelyardStatusManagerModule) AppConst.getModuleManager().peekModule(SteelyardStatusManagerModule.class);
        if (statusManagerModule != null ) {
            statusInfo = statusManagerModule.getStatusInfoBySteelyardMac(RunTimeData.steelyardInfo.getSteelyardMac());
        }

        JsonObject configjson = new JsonObject();
        configjson.addProperty("mac",steelyardValue.steelyardMac);

        if(statusInfo != null){
            configjson.addProperty("romv",statusInfo.getVersion());
            steelyardValue.exit_factory_id = statusInfo.getNumno();
        }
        configjson.addProperty("online_code", AppConst.deviceUncode);
        configjson.addProperty("deviceUncode", AppConst.deviceUncode);

        configjson.addProperty("appv", AppHelp.getLocalVersionName());
        stringStringMap.put("configjson",configjson.toString());

        stringStringMap.put("moid",15);
        stringStringMap.put("exit_factory_id",steelyardValue.exit_factory_id);
        stringStringMap.put("balance_no",steelyardValue.steelyardMac);

        stringStringMap.put("mac_address",WifiUtils.getInstance().getLocalMacAddress());

        try {
            stringStringMap.put("ccid",new PhoneInfoUtils(AppConst.getApplication()).getIccid());
        }catch (Exception e){}


        String json = AppApi.regBalanceTask(stringStringMap, this);

        ResponEntity<Void> responEntity = ResponEntity.fromJson(json, null, "");

        if (responEntity.isSuccess()) {
            complete(responEntity.isSuccess());
        } else {
            
            if(json == null){
                requestRe();
            }else {
                complete(false);
            }

        }


    }


    
    int isReForNetOK = 0;
    private void requestRe() {
        if (!getIsContinue()) return;

        if(isReForNetOK >= 5){
            complete(false);
            return;
        }
        
        if (NetworkStateModule.getInstance().getIsNetworkOK()) {
            isReForNetOK ++;
            doExecuting(null);
        } else {
            
            if (observerAutoNext != null)
                LinkBus.unregisterReceiver(observerAutoNext);
            LinkBus.registerReceiver(getobserverAutoNext());
        }
    }

    ObserverAutoNext observerAutoNext;

    private ObserverAutoNext getobserverAutoNext() {
        if (observerAutoNext == null) {
            observerAutoNext = new ObserverAutoNext<OnNetworkStateEvent>() {
                @Override
                public void onReceiver(OnNetworkStateEvent data) {
                    if (!getIsContinue()) {
                        LinkBus.unregisterReceiver(this);
                        return;
                    }
                    if (data.extIsNetworkOK) {
                        LinkBus.unregisterReceiver(this);
                        doExecuting(null);
                    }
                }

                @Override
                public Class getBusEventClass() {
                    return OnNetworkStateEvent.class;
                }
            };
        }
        return observerAutoNext;
    }

    @Override
    public boolean onInterceptEnd(boolean isCancel, Boolean result) {
        if (observerAutoNext != null)
            LinkBus.unregisterReceiver(observerAutoNext);
        return super.onInterceptEnd(isCancel, result);
    }

    







































}